/**
 * 跑马灯组件
 */
class LampCompUI extends BaseComp{
    private lb_tips:eui.Label;
    private lb_tips1:eui.Label;
    private timer:egret.Timer;

    private winList = [];
    private worldList = [];
    private curIndex = 0;
    constructor()
    {
        super('LampCompSkin');
    }

    public init()
    {
        this.width = 500;
        this.height = 55;
        this.timer = new egret.Timer(30);
        this.requestInfo();

        // if(TimeUtil.getIsNight())
        // {
        //     this.lb_tips.textColor = this.lb_tips1.textColor = 0xcccccc;
        // }
        // this.timer.start();
    }

    protected requestInfo()
    {
        let data = {
            version:GameConst.version
        }
        let self = this;
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorendless/get_lucky_marquee' , StringUtil.jsonToHttpPostString(data), (e)=>{
            let da = JSON.parse(e);
            if(da && da.data)
            {
                self.winList = da.data;
                self.showTips();
                /**
                 * 不知道什么原因，同时发送两个http请求收不到回调，所以先将这个红点初始化放在这里，后续解决这个bug
                 * TODO
                 */
                if(RoleCache.instance().isGetLunchAward == null)
                {
                    ActivityPrase.instance().requestActivityInfo(null, null);
                }
            }
        })
    }

    public showTips()
    {
        let len = this.winList.length;
        if(len > 0)
        {
            for(let i=0;i<len;i++)
            {
                let num:string = this.winList[i];
                let str = StringUtil.getSwfLangStrVar('win_1', [num]);  
                this.worldList.push(str);                                                                                                                                                            
            }
            this.lb_tips1.text = this.worldList[this.curIndex];
        }
        
        egret.setTimeout(()=>{
            this.timer.start();
        },this, 500);
    }

    public addEvent()
    {
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.update, this);
    }

    public update()
    {
        let speed = 3;
        this.lb_tips.x -= speed;
        this.lb_tips1.x -= speed;
        if(this.lb_tips.x < -this.lb_tips.width)
        {
            if(this.curIndex >= this.worldList.length)
            {
                this.curIndex = -1;
            }
            this.lb_tips.text = this.worldList[++this.curIndex];
            this.lb_tips.x = this.lb_tips1.x + 1.3 * this.lb_tips1.width;
        }
        if(this.lb_tips1.x < -this.lb_tips1.width)
        {
            if(this.curIndex >= this.worldList.length)
            {
                this.curIndex = -1;
            }
            this.lb_tips1.text = this.worldList[++this.curIndex];
            this.lb_tips1.x = this.lb_tips.x + 1.3 * this.lb_tips.width;
        }
    }

    public removeEvent()
    {
        this.timer.removeEventListener(egret.TimerEvent.TIMER, this.update, this);
    }

    public clear()
    {
        super.clear();
        this.timer.stop();
        this.timer = null;
        this.winList = null;
    }
}

window['LampCompUI'] = LampCompUI;